package com.gxpt.app.view;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.R;
import com.gxpt.app.bean.normal;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_reg)
public class Activity_reg extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.checkcode)
    TextView checkcode;

    @ViewInject(R.id.put_phone)
    EditText put_phone;

    @ViewInject(R.id.put_code)
    EditText put_code;

    @ViewInject(R.id.toNext)
    Button toNext;


    private MyCountDownTimer mc;

    boolean isLookforPwd = false;
    int opt=1;
    Bundle d = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        d            = getIntent().getBundleExtra("DATA");
        if(d!=null){
            isLookforPwd = d.getBoolean("isLookforPwd",false);
        }
        initView();
    }

    @Override
    public void initView() {
        if(isLookforPwd){
            txt_title.setText(getString(R.string.txt_login_backpwd));
        }else{
            txt_title.setText(getString(R.string.txt_reg));
        }
    }

    @Override
    public void initData() {

    }

    public boolean checkPhone() {
        if (put_phone.getText().toString().equals("")) {
            T.show(context, getResources().getString(R.string.alert_putPhone), 1);
            return false;
        } else if (put_phone.getText().toString().length() < 11) {
            T.show(context, getResources().getString(R.string.alert_putPhone_Good), 1);
            return false;
        } else {
            return true;
        }
    }


    private void lastTime(){
        checkcode.setEnabled(false);
        mc = new MyCountDownTimer(1000*60, 1000);
        mc.start();
    }


    @Event(value={R.id.toback,R.id.checkcode,R.id.toNext})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
            case R.id.checkcode:
                if(checkPhone()){
                    toNet_sendyzm();
                }

                break;
            case R.id.toNext:
                Bundle b = new Bundle();
                b.putString("mem_tel"       ,put_phone.getText().toString());
                b.putString("smsyzm"        ,put_code.getText().toString());
                b.putBoolean("isLookforPwd" ,isLookforPwd);

                jumpActivity(Activity_set_pwd.class,b,false);
                break;
        }
    }

    //////NET DO
    StringCallback do_sendyzm = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                normal data = new Gson().fromJson(s, normal.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    lastTime();
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };

    /////NET
    private void toNet_sendyzm(){
        hud.show();
        if(isLookforPwd){
            opt = 2;
        }else{
            opt = 1;
        }
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "member");
            jsonBody.put("serviceName",          "sendyzm");

            jsonBody.put("mem_tel",              put_phone.getText().toString());
            jsonBody.put("active_time",           3600);
            jsonBody.put("opt",              opt);//0：其它；1：注册；2：忘记密码；
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_sendyzm+" ]");
        HttpUtils.http_post_string(Constant.URL_sendyzm,jsonBody.toString(),do_sendyzm);
    }

























    /**
     * 继承 CountDownTimer 防范
     *
     * 重写 父类的方法 onTick() 、 onFinish()
     */

    class MyCountDownTimer extends CountDownTimer {
        /**
         *
         * @param millisInFuture
         *      表示以毫秒为单位 倒计时的总数
         *
         *      例如 millisInFuture=1000 表示1秒
         *
         * @param countDownInterval
         *      表示 间隔 多少微秒 调用一次 onTick 方法
         *
         *      例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         *
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
             checkcode.setText(getResources().getString(R.string.txt_sendCheckNo));
            checkcode.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {

            checkcode.setText(""+millisUntilFinished / 1000+"秒后重新发送");
        }
    }
}
